package com.source.utils;

import com.alibaba.fastjson.JSON;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @Author ZZF
 * @Time 2021/03/13 14:12
 * 响应工具类
 */
public class ResponseUtils {

    public static void placeToken(HttpServletResponse httpServletResponse,String token){
        httpServletResponse.setHeader("Authorization",token);
        httpServletResponse.setHeader("Access-Control-Expose-Headers","Authorization");
    }

    /**
     * 返回通用格式给前端
     * @param response
     * @param commonResult
     * @throws IOException
     */
    public static void write(HttpServletResponse response,CommonResult commonResult) throws IOException {
        response.getWriter().write(JSON.toJSONString(commonResult));
    }

    public static void solveEncoding(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

    }



}
